/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.easyedu.dictionary.explorer;

import eu.easyedu.dictionary.api.model.Dictionary;
import eu.easyedu.dictionary.api.service.events.DictionaryEventListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author hlavki
 */
public class DictionaryList {

    private static DictionaryList instance;
    private Lookup.Result<Dictionary> result = getLookupResult();
    private final List<DictionaryEventListener> listeners = new ArrayList<DictionaryEventListener>(1);

    private DictionaryList() {
        // issue 75204: forces the DataObject's corresponding to the DatabaseConnection's
        // to be initialized and held strongly so the same DatabaseConnection is
        // returns as long as it is held strongly
        result.allInstances();

        result.addLookupListener(new LookupListener() {

            public void resultChanged(LookupEvent e) {
                fireListeners();
            }
        });
    }

    public static synchronized DictionaryList getDefault() {
        if (instance == null) {
            instance = new DictionaryList();
        }
        return instance;
    }

    public Dictionary[] getDictionariesArray() {
        Collection<? extends Dictionary> dicts = result.allInstances();
        return dicts.toArray(new Dictionary[dicts.size()]);
    }

    public Collection<? extends Dictionary> getDictionaries() {
        return result.allInstances();
    }

    public Dictionary getDictionary(Dictionary impl) {
        if (impl == null) {
            throw new NullPointerException();
        }
        Collection<? extends Dictionary> dicts = getDictionaries();
        for (Dictionary dictionary : dicts) {
            if (impl.equals(dictionary)) {
                return dictionary;
            }
        }
        return null;
    }

    public void add(Dictionary dictionary) throws IOException {
        if (dictionary == null) {
            throw new NullPointerException();
        }
        DictionaryConvertor.create(dictionary);
    }

    public boolean contains(Dictionary dictionary) {
        return getDictionary(dictionary) != null;
    }

    public void remove(Dictionary dictionary) throws IOException {
        if (dictionary == null) {
            throw new NullPointerException();
        }
        DictionaryConvertor.remove(dictionary);
    }

    public void addDictionaryEventListener(DictionaryEventListener listener) {
        synchronized (listeners) {
            listeners.add(listener);
        }
    }

    public void removeDictionaryEventListener(DictionaryEventListener listener) {
        synchronized (listeners) {
            listeners.remove(listener);
        }
    }

    private void fireListeners() {
        List<DictionaryEventListener> listenersCopy;

        synchronized (listeners) {
            listenersCopy = new ArrayList<DictionaryEventListener>(listeners);
        }
        for (DictionaryEventListener listener : listenersCopy) {
            listener.dictionaryChanged();
        }
    }

    private synchronized Lookup.Result<Dictionary> getLookupResult() {
        return Lookups.forPath(DictionaryConvertor.DICTIONARIES_PATH).lookupResult(Dictionary.class);
    }


}

